//
//  ViewController.m
//  猜图游戏
//
//  Created by song on 15/1/7.
//  Copyright (c) 2015年 song. All rights reserved.
//

#import "ViewController.h"
#import "JOQuestion.h"
@interface ViewController () <UIAlertViewDelegate>

@property(nonatomic , weak ) UIButton *cover;

@property(nonatomic ,assign ) CGRect  origin;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.origin = self.imgbtn.frame;
    
    self.index =-1;
    // 加载第一条记录
    [self nextquestes:nil];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(NSArray *)questdatas
{
    
    if(_questdatas ==nil)
    {
        NSBundle *mainBundle = [NSBundle mainBundle];
        NSString *path = [mainBundle pathForResource:@"questions.plist" ofType:nil];
        NSArray *data = [NSArray arrayWithContentsOfFile:path];
        NSMutableArray *tmparray = [[NSMutableArray alloc] initWithCapacity:0];
        for(NSDictionary *dic  in data)
        {
            JOQuestion *question =[[JOQuestion alloc]init];
            question.answer  = dic[@"answer"];
            question.icon = dic[@"icon"];
            question.title= dic[@"title"];
            question.options= dic[@"options"];
            [tmparray addObject:question];
        }
        
        _questdatas = tmparray;
        
    }
    
    return _questdatas;
}


-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag ==10) {
         NSLog(@"点击了 第 %lu",buttonIndex);
    }
}


- (IBAction)tip:(id)sender {
    
    for(UIButton *btn in self.answerview.subviews)
    {
        [self answerclick:btn];
        
    }
    
    JOQuestion *question = self.questdatas[self.index];
    
    
    NSString *first = [question.answer substringToIndex:1];
    
    for(UIButton *btn in self.optionview.subviews)
    {
        if([btn.currentTitle isEqualToString:first])
        {
            [self selectanswer:btn];
            break;
        }
    }
    
    int score = [self.scorebtn titleForState:UIControlStateNormal].intValue;
    score -= 200;
    
    [self.scorebtn setTitle:[NSString stringWithFormat:@"%d",score] forState:UIControlStateNormal];
    
//    [self performSelector:@selector(nextquestes:) withObject:self afterDelay:1.0];
    
}

- (IBAction)help:(id)sender {
    

}

- (IBAction)bigImage:(id)sender {
    
    
    [UIView  beginAnimations:nil context:nil];
    [UIView setAnimationDelay:1.0];
    [UIView setAnimationRepeatCount:1];
    UIButton *cover= [[UIButton alloc] init];
    cover.frame = self.view.bounds;
    cover.backgroundColor = [UIColor blackColor];
    cover.alpha = 0.7;
    _cover =cover;
    [cover addTarget:self action:@selector(smallimg:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:cover];
  
    
    self.imgbtn.frame = CGRectMake(0, 80, self.view.frame.size.width, self.view.frame.size.width);
    [self.view bringSubviewToFront: self.imgbtn];
    
    [UIView commitAnimations];

}

-(void)smallimg:(UIButton *)sender
{


    [UIView animateWithDuration:1.0 animations:^{
        
        self.cover.alpha =0.0;      //透明状态
        self.imgbtn.frame = _origin; //CGRectMake(85, 113, 150, 150);
        
    } completion:^(BOOL finished) {
        [self.cover removeFromSuperview];   //删除这个按钮
        self.cover =nil;
        
    }];
}




- (IBAction)nextquestes:(id)sender {
    
    
    self.index++;
    
    if(self.index == self.questdatas.count-1)
    {
//        UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"恭喜" message:@"通关了！！ 尽请期待" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"好的", nil];
//        [alert show];
//        alert.tag = 10;
//        
//        UIAlertView *alert2 =[[UIAlertView alloc] initWithTitle:@"恭喜" message:@"通关了！！ 尽请期待" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"好的", nil];
//        [alert2 show];
//        alert2.tag = 20;
        UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:@"恭喜通关" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:@"其他", nil];
        [sheet showInView:self.view];
        
        
    }
    
    
    JOQuestion *question  = self.questdatas[_index];
    self.titleLable.text = question.title;
    [self.imgbtn setImage:[UIImage imageNamed:question.icon] forState:UIControlStateNormal];
    self.postionLable.text = [NSString stringWithFormat:@"%d/%d" , _index+1 , (int)_questdatas.count];
    self.nextbtn.enabled =( _index !=(_questdatas.count -1));
    
    // 先删除之前的按钮
    [self.answerview.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
//    for(UIView *view  in self.answerview.subviews)
//    {
//        [view removeFromSuperview];
//    }
    
    for(UIView *view  in self.optionview.subviews )
    {
        [view removeFromSuperview];
    }
    
    int  margin = 10;
    int len = question.answer.length;
    int left = ( self.view.frame.size.width -(len -1)*margin  - (len*35))*0.5;
    
    for(int i =0 ;i<  question.answer.length ;i++)
    {
        UIButton *btn =[[UIButton alloc ]init];
        
        [btn  setBackgroundImage:[UIImage imageNamed:@"btn_answer"] forState:UIControlStateNormal];
        [btn  setBackgroundImage:[UIImage imageNamed:@"btn_answer_highlighted"] forState:UIControlStateHighlighted];
        
        int x = left + (i+1 )* margin +i *35;
        btn.frame = CGRectMake(x, 0, 35, 35);
        
        [btn addTarget:self action:@selector(answerclick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.answerview addSubview:btn];
    }
    
    int count = question.options.count;
    int rowsize = 7;
    int viewsize =  self.view.frame.size.width;
    int optionW = 35;
    int optionH =35;
    
    
    for(int  i = 0 ;i<count; i++)
    {
        UIButton *btn = [[UIButton alloc] init];
        
        [btn  setBackgroundImage:[UIImage imageNamed:@"btn_answer"] forState:UIControlStateNormal];
        [btn  setBackgroundImage:[UIImage imageNamed:@"btn_answer_highlighted"] forState:UIControlStateHighlighted];
        
        int col = i % rowsize;
        int row = i / rowsize;
        int optionleft = (viewsize -rowsize*optionW - (rowsize -1 ) *margin )* 0.5;
        
        int optionX = optionleft + col*(optionW +margin);
        int optionY =row *(optionH +margin);
        
        btn.frame = CGRectMake(optionX, optionY, optionW, optionH);
        
        [btn setTitle:question.options[i]  forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(selectanswer:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.optionview addSubview:btn];
        
    }
    
    

}


-(void) selectanswer:(UIButton *)sender
{
    

    
    NSMutableString *datastr =[[NSMutableString alloc] initWithCapacity:0];
    JOQuestion  *ques = self.questdatas[self.index];
    NSString *stitle = [sender titleForState:UIControlStateNormal];
    BOOL isfull = YES;
    int size = 0;
    for (UIButton *btn in  self.answerview.subviews)
    {
        NSString *title = [btn titleForState:UIControlStateNormal];
        
        if(title ==nil || title.length==0)
        {
            [btn setTitle:stitle forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            sender.hidden = YES;
            break;
        }
    
    }
 
    for (UIButton *btn in  self.answerview.subviews)
    {
        NSString *title = [btn titleForState:UIControlStateNormal];
        
        if(title ==nil || title.length==0)
        {
            isfull =NO;
            break;
        }
    }
    
    
    if(isfull)
    {
        NSString *answer =  ques.answer;
        for (UIButton *btn in  self.answerview.subviews)
        {
            NSString *title = [btn titleForState:UIControlStateNormal];
            
            [datastr appendString:title];
        }
        
        if([datastr isEqualToString:answer])
        {
            NSLog(@"right answer@@ !!");
            
            int score = [self.scorebtn titleForState:UIControlStateNormal].intValue;
            score += 100;
            
            [self.scorebtn setTitle:[NSString stringWithFormat:@"%d",score] forState:UIControlStateNormal];
            
            [self performSelector:@selector(nextquestes:) withObject:self afterDelay:1.0];
            
            
            
        }
    
    }
    
}


-(void) answerclick:(UIButton *)sender
{
    NSString *stitle = [sender titleForState:UIControlStateNormal];
    
    for (UIButton *btn in  self.optionview.subviews)
    {
        NSString *title = [btn titleForState:UIControlStateNormal];
        
        if( [stitle isEqualToString:title] && btn.hidden == YES)
        {
             btn.hidden = NO;
            [btn setTitle:stitle forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [sender setTitle:@"" forState:UIControlStateNormal];
           
            break;
        }
    }
    
}



- (IBAction)iconclick:(UIButton *)sender {
    
    if(self.cover==nil)
    {
        [self bigImage:nil];
    }else{
        [self smallimg:nil];
    }
    
    
}


@end
